package com.example.yichuangyun.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ItemEventLayoutBinding;
import com.example.yichuangyun.databinding.LayoutEmptyListBinding;
import com.example.yichuangyun.model.index.DaiBanDataModel;
import com.example.yichuangyun.ui.activity.business.BusinessDetailsActivity;
import com.example.yichuangyun.ui.activity.follow.AddFollowActivity;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.JumpIntent;

import java.util.ArrayList;
import java.util.List;

public class DaiBanEventAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * viewType--分别为item以及空view
     */
    private final int VIEW_TYPE_ITEM = 1;
    private static final int VIEW_TYPE_EMPTY = 0;
    private final Context context;
    private List<DaiBanDataModel.RecordsBean> mData;
    private OnCallListener onCallListener;

    public DaiBanEventAdapter(Context context, List<DaiBanDataModel.RecordsBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    public void onRefresh(List<DaiBanDataModel.RecordsBean> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void addData(List<DaiBanDataModel.RecordsBean> mList) {
        if (mList != null && mList.size() != 0) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在这里根据不同的viewType进行引入不同的布局
        if (viewType == VIEW_TYPE_EMPTY) {
            LayoutEmptyListBinding emptyListBinding = LayoutEmptyListBinding.inflate(LayoutInflater.from(context), parent, false);
            return new EmptyViewHolder(emptyListBinding);
        } else {
            ItemEventLayoutBinding itemEventLayoutBinding = ItemEventLayoutBinding.inflate(LayoutInflater.from(context), parent, false);
            return new MyViewHolder(itemEventLayoutBinding);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            DaiBanDataModel.RecordsBean model = mData.get(position);
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.binding.tvName.setText(TextUtils.isEmpty(model.getUserName()) ? context.getString(R.string.nameless) : model.getUserName());
            myViewHolder.binding.tvPhoneNum.setText("· " + model.getTelNumber());
            myViewHolder.binding.ivFollow.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.flagKey, 0);
                bundle.putString(Constant.businessIdKey, model.getId());
                bundle.putString(Constant.nameKey, model.getUserName());
                bundle.putString(Constant.phoneKey, model.getTelNumber());
                JumpIntent.jump((Activity) context, AddFollowActivity.class, bundle);
            });

            myViewHolder.binding.ivCall.setOnClickListener(v -> {
                if (CommonUtil.isCanClick()) {
                    if (onCallListener != null) {
                        onCallListener.callListener(model);
                    }
                }
            });
            myViewHolder.itemView.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.businessIdKey, model.getId());
                JumpIntent.jump((Activity) context, BusinessDetailsActivity.class, bundle);
            });
        } else if (holder instanceof EmptyViewHolder) {
            EmptyViewHolder emptyHolder = (EmptyViewHolder) holder;
            emptyHolder.binding.text.setText(context.getString(R.string.no_to_do_event));
            emptyHolder.binding.ivNoData.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        //同时这里也需要添加判断，如果mData.size()为0的话，只引入一个布局，就是emptyView
        // 那么，这个recyclerView的itemCount为1
        if (mData.size() == 0) {
            return 1;
        }
        //如果不为0，按正常的流程跑
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        //在这里进行判断，如果我们的集合的长度为0时，我们就使用emptyView的布局
        if (mData.size() == 0) {
            return VIEW_TYPE_EMPTY;
        }
        //如果有数据，则使用ITEM的布局
        return VIEW_TYPE_ITEM;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemEventLayoutBinding binding;

        public MyViewHolder(ItemEventLayoutBinding itemEventLayoutBinding) {
            super(itemEventLayoutBinding.getRoot());
            binding = itemEventLayoutBinding;
        }
    }

    static class EmptyViewHolder extends RecyclerView.ViewHolder {

        LayoutEmptyListBinding binding;

        public EmptyViewHolder(LayoutEmptyListBinding emptyListBinding) {
            super(emptyListBinding.getRoot());
            binding = emptyListBinding;
        }
    }

    public interface OnCallListener {
        void callListener(DaiBanDataModel.RecordsBean model);
    }

    public void setOnCallListener(OnCallListener onCallListener) {
        this.onCallListener = onCallListener;
    }
}
